/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package dao;

import entity.Pekerjaan;
import java.util.ArrayList;
import java.util.List;
import javax.persistence.EntityManager;

/**
 *
 * @author Darkman
 */
public class PekerjaanDAOImpl extends GeneralDAOImpl implements PekerjaanDAO {

    public PekerjaanDAOImpl(EntityManager em) {
        super(em);
    }

    public Pekerjaan get(Long id) throws Exception {
        Pekerjaan p = null;

        try {
            em.getTransaction().begin();
            p = em.find(Pekerjaan.class, id);
            em.getTransaction().commit();
        } catch (Exception e) {
            throw e;
        }

        return p;
    }

    public List<Pekerjaan> gets() throws Exception {
        List<Pekerjaan> list = new ArrayList<Pekerjaan>();

        try {
            em.getTransaction().begin();
            list = em.createQuery("SELECT p FROM Pekerjaan p").getResultList();
            em.getTransaction().commit();
        } catch (Exception e) {
            throw e;
        }

        return list;
    }

    public List<Pekerjaan> findPekerjaan(String pekerjaan) throws Exception {
        pekerjaan = "%" + pekerjaan + "%";
        List<Pekerjaan> list = new ArrayList<Pekerjaan>();
        try {
            em.getTransaction().begin();
            list = em.createQuery("SELECT p FROM Pekerjaan p WHERE p.nama LIKE :n").setParameter("n", pekerjaan).getResultList();
            em.getTransaction().commit();
        } catch (Exception e) {
            throw e;
        }
        return list;
    }
}
